//
//  ICMSelecteTimeModel.m
//  ICrEAM
//
//  Created by rainimute on 16/11/3.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ICMSelecteTimeModel.h"

@implementation ICMSelecteTimeModel

- (void)didSelectedTimeButtonIndex:(NSInteger)index{
    if ([self.currentSelectedTimes containsObject:[@(index)stringValue]]) {
        if ([self.currentSelectedTimes containsObject:[@(index-1)stringValue]]) {
            for (NSInteger i=index; i<52; i++) {
                [self.currentSelectedTimes containsObject:[@(i)stringValue]]?[self.currentSelectedTimes removeObject:[@(i)stringValue]]:@"";
            }
        }else{
            [self.currentSelectedTimes removeObject:[@(index)stringValue]];
        }
    }else{
        if ([self.currentSelectedTimes containsObject:[@(index+1)stringValue]]||[self.currentSelectedTimes containsObject:[@(index-1)stringValue]]||(self.currentSelectedTimes.count==0)) {
            [self.currentSelectedTimes addObject:[@(index)stringValue]];
        }
    }
}

- (NSMutableArray *)currentSelectedTimes{
    if (!_currentSelectedTimes) {
        _currentSelectedTimes = [NSMutableArray array];
    }
    return _currentSelectedTimes;
}

- (NSMutableArray *)haveSelectedTimes{
    if (!_haveSelectedTimes) {
        _haveSelectedTimes = [NSMutableArray array];
    }
    return _haveSelectedTimes;
}

- (NSMutableArray *)onlineSourceTimes{
    if (!_onlineSourceTimes) {
        _onlineSourceTimes = [NSMutableArray array];
    }
    return _onlineSourceTimes;
}

- (NSInteger)lastServerIndex{
    _lastServerIndex = 0;
    for (NSString *selectedTime in self.currentSelectedTimes) {
        NSInteger selectedIndex = [selectedTime integerValue];
        _lastServerIndex = _lastServerIndex>selectedIndex?_lastServerIndex:selectedIndex;
    }
    return _lastServerIndex;
}

- (NSString *)firstTimeDesc{
    NSInteger firstIndex = 100;
    for (NSString *selectedTime in self.currentSelectedTimes) {
        NSInteger selectedIndex = [selectedTime integerValue];
        firstIndex = firstIndex>selectedIndex?selectedIndex:firstIndex;
    }
    NSArray *timeStrings = @[@"7:00",@"7:15",@"7:30",@"7:45",@"8:00",@"8:15",@"8:30",@"8:45",@"9:00",@"9:15",@"9:30",@"9:45",@"10:00",@"10:15",@"10:30",@"10:45",@"11:00",@"11:15",@"11:30",@"11:45",@"12:00",@"12:15",@"12:30",@"12:45",@"13:00",@"13:15",@"13:30",@"13:45",@"14:00",@"14:15",@"14:30",@"14:45",@"15:00",@"15:15",@"15:30",@"15:45",@"16:00",@"16:15",@"16:30",@"16:45",@"17:00",@"17:15",@"17:30",@"17:45",@"18:00",@"18:15",@"18:30",@"18:45",@"19:00",@"19:15",@"19:30",@"19:45"];
    if (self.currentSelectedTimes.count) {
        return timeStrings[firstIndex];
    }
    return nil;
}

- (instancetype)initSelecteTimeModelWithDataDic:(NSDictionary*)dataDic{
    if (self = [super init]) {
        self.isOnlineSource = [dataDic[@"DATA"][@"STATUS"] isEqualToString:@"1"];
        if ([dataDic[@"DATA"][@"TIME_SEQ"] isKindOfClass:[NSString class]]) {
            NSArray *times;
            if (self.isOnlineSource) {
                times = [[dataDic[@"DATA"][@"TIME_SEQ"] componentsSeparatedByString:@","]mutableCopy];
                for (NSString *timeSeq in times) {
                    [self.onlineSourceTimes addObject:[@([timeSeq integerValue]) stringValue]];
                }
            }else{
                times = [[dataDic[@"DATA"][@"TIME_SEQ"] componentsSeparatedByString:@","]mutableCopy];
                for (NSString *timeSeq in times) {
                    [self.haveSelectedTimes addObject:[@([timeSeq integerValue]) stringValue]];
                }
            }
        }
        self.stationCode = dataDic[@"LABOUR_POSITION_CODE"];
    }
    return self;
}

+ (instancetype)createSelecteTimeModelWithDataDic:(NSDictionary*)dataDic{
    return [[ICMSelecteTimeModel alloc]initSelecteTimeModelWithDataDic:dataDic];
}

@end
